from django.contrib import admin
from app_article.models import Article, Category, Tags, UpOrDown, Comment


@admin.register(Article)
class Article(admin.ModelAdmin):
    def display_content(self, obj):
        return obj.content[:20]

    display_content.short_description = '文章正文'

    def display_desc(self, obj):
        return obj.desc[:10]

    def display_tags(self, obj):
        tag_all = obj.tags.all()
        return '\n'.join([tag.tag for tag in tag_all])

    display_desc.short_description = '文章简介'
    ordering = ['id']
    list_display = ['id', 'title', 'display_desc', 'display_content', 'create_time', 'up_count', 'down_count',
                    'comment_count', 'blog',
                    'category', 'display_tags']


@admin.register(Category)
class Category(admin.ModelAdmin):
    list_display = ['name', 'blog']


@admin.register(Tags)
class Tags(admin.ModelAdmin):
    list_display = ['name', 'blog']


@admin.register(UpOrDown)
class UpOrDown(admin.ModelAdmin):
    list_display = ['article', 'user', 'is_up']


@admin.register(Comment)
class Comment(admin.ModelAdmin):
    list_display = ['article', 'user', 'content', 'parent']
